package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {


    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;
    //图片
    @Autowired
    private GraphInfoMapper graphInfoMapper;

    //属性信息
    @Autowired
    private AttrValueMapper attrValueMapper;

    //配套
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    //标签
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    //支付方式
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    //杂费列表
    @Autowired
    private FeeValueMapper feeValueMapper;

    //租期
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Override
    public Page<RoomItemVo> pageForRoom(Page<RoomItemVo> page, RoomQueryVo queryVo) {
       return  roomInfoMapper.selectRoomByPage(page,queryVo);
    }

    //根据房间id查询详细信息
    @Override
    public RoomDetailVo queryByRoomId(Long id) {
        //查询房间信息
        RoomInfo roomInfo=roomInfoMapper.selectByRoomId(id);

        if(ObjectUtils.isEmpty(roomInfo)){
            return null;
        }
        //根据房间id查询房间所属公寓
        ApartmentItemVo apartmentItemVo= apartmentInfoService.queryApartmentByApartId(roomInfo.getApartmentId());
        //查询图片列表
        List<GraphVo> graphVoList= graphInfoMapper.selectByRoomId(ItemType.ROOM,id);
        //查询属性信息列表
        List<AttrValueVo> attrValueVoList=attrValueMapper.selectByRoomId(id);
        //查询配套信息列表
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectByRoomId(id);
        //查询标签信息列表
        List<LabelInfo> labelInfoList=labelInfoMapper.selectByRoomId(id);
        //查询支付方式
        List<PaymentType> paymentTypeList=paymentTypeMapper.selectByRoomId(id);
        //查询杂费信息列表
        List<FeeValueVo> feeValueVoList=feeValueMapper.selectByApartmentId(roomInfo.getApartmentId());
        //租期列表
        List<LeaseTerm> leaseTermList=leaseTermMapper.selectByRoomId(id);
        //查询房间状态
        LambdaQueryWrapper<LeaseAgreement> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(LeaseAgreement::getRoomId,roomInfo.getId());
        roomInfoLambdaQueryWrapper.in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        Long aLong = leaseAgreementMapper.selectCount(roomInfoLambdaQueryWrapper);

        //
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        roomDetailVo.setIsCheckIn(aLong > 0);

        //设置公寓信息
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        //设置图片
        roomDetailVo.setGraphVoList(graphVoList);
        //设置属性信息
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        //设置配套信息
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        //设置标签信息
        roomDetailVo.setLabelInfoList(labelInfoList);
        //设置支付方式
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        //设置杂费信息
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        //设置租期信息
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    //根据公寓id查询房间分页信息
    @Override
    public Page<RoomItemVo> pageByApartmentId(Page<RoomItemVo> page, Long id) {

        return roomInfoMapper.selectRoomByPageAndApartmentId(page,id);
    }

    //根据房间id查询租期列表
    @Override
    public List<LeaseTerm> queryByLeaseRoomId(Long id) {

        return roomInfoMapper.selectLeaseByRoomId(id);
    }
}




